package com.springboot.springbootlogindemo.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.springbootlogindemo.DTO.UserLikCountDTO;
import com.springboot.springbootlogindemo.DTO.UserLikesDto;
import com.springboot.springbootlogindemo.domain.UserLikes;
import com.springboot.springbootlogindemo.mapper.UserLikesMapper;
import com.springboot.springbootlogindemo.utils.Constants.RedisConstants;
import com.springboot.springbootlogindemo.utils.eum.LikedStatusEum;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class RedisUtils {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private UserLikesMapper userLikesDao;


    /**
     * 点赞
     *
     * @param infoId
     * @param likeUserId
     */
    private void likes(String infoId,String likeUserId) {
        //拼接被点赞和点赞对象
        String likedKey = RedisConstants.getRecordKey(infoId, likeUserId);

        //存入点赞数量表，是一个hash类型，key是infoId ,记录点赞次数为1
        redisTemplate.opsForHash().increment(RedisConstants.LIKED_COUNT, infoId, 1);

        //存入点赞记录表，是一个hash类型，key是拼接的likedKey，值是1
        redisTemplate.opsForHash().put(RedisConstants.LIKED_RECORD, likedKey, LikedStatusEum.LIKE.getCode());
    }


    /**
     * 取消点赞
     *
     * @param infoId
     * @param likeUserId
     */
    private void unLikes(String infoId,String likeUserId) {

        //拼接被点赞和点赞对象
        String likedKey = RedisConstants.getRecordKey(infoId, likeUserId);

        //存入点赞数量表，是一个hash类型，key是infoId ,记录点赞次数为 -1
        redisTemplate.opsForHash().increment(RedisConstants.LIKED_COUNT, infoId, -1);

        //删除点赞记录
        redisTemplate.opsForHash().delete(RedisConstants.LIKED_RECORD, likedKey);
    }


    /**
     * 判断点赞状态，然后进行后面的操作
     *
     * @param infoId
     * @param likeUserId
     * @return
     */
    public Object likeStatus(String infoId, String likeUserId) {
        //如果有点赞记录表，并且点赞记录表中有该点赞记录
        if (redisTemplate.opsForHash().hasKey(RedisConstants.LIKED_RECORD, RedisConstants.getRecordKey(infoId, likeUserId))) {

            //获取该点赞记录
            String o = redisTemplate.opsForHash().get(RedisConstants.LIKED_RECORD, RedisConstants.getRecordKey(infoId, likeUserId)).toString();

            //如果是点赞，那么取消点赞
            if ("1".equals(o)) {
                unLikes(infoId, likeUserId);
                return LikedStatusEum.UNLIKE;
            }
            //如果没有点赞，那么点赞
            if ("0".equals(o)) {
                likes(infoId,likeUserId);
                return LikedStatusEum.LIKE;
            }
        }

        //如果没有点赞记录表，或者点赞记录表中没有该点赞记录（redis中没有），从数据库中查找
        UserLikes userLikes = userLikesDao.selectOne(new QueryWrapper<UserLikes>().eq("info_id", infoId).eq("like_user_id", likeUserId));

        //查不到
        if (userLikes == null) {

            //创建一个点赞记录
            UserLikes userLikes1 = new UserLikes();
            userLikes1.setInfoId(Long.valueOf(infoId));
            userLikes1.setLikeUserId(Long.valueOf(likeUserId));

            userLikes1.setStatus(1);
            userLikesDao.insert(userLikes1);
            //存入redis
            likes(infoId,likeUserId);
            return LikedStatusEum.LIKE;
        }

        if (userLikes.getStatus() == 1) {
            unLikes(infoId,likeUserId);
            return LikedStatusEum.UNLIKE;
        }

        if (userLikes.getStatus() == 0) {
            likes(infoId, likeUserId);
            return LikedStatusEum.LIKE;
        }
        return "";
    }



}